package com.qingrongby.shop.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @CLASSNAME JwtTokenUtil
 * @DESCRIPTION TODO
 * @AUTHOR hzp
 * @DATE 2020/7/9
 * @VERSION 1.0
 **/
@Component
public class JwtTokenUtil {

    private static String secret;
    private static Integer hour;

    @Value("${jwt.secret}")
    public void setSecret(String secret) {
        JwtTokenUtil.secret = secret;
        System.out.println(secret);
        System.out.println(JwtTokenUtil.secret);
    }

    @Value("${jwt.hour}")
    public void setHour(Integer hour) {
        JwtTokenUtil.hour = hour;
    }

    public static String creatToken() {
        Algorithm algorithm = Algorithm.HMAC256(JwtTokenUtil.secret);
        String token = JWT.create()
                .withExpiresAt(DateUtils.addDate(new Date(), JwtTokenUtil.hour))
                .withClaim("uid", "3")
                .withClaim("scope", "3")
                .withClaim("secerty", "3")
                .sign(algorithm);
        return token;
    }

    /*验证token*/
    public static Map<String, Claim> verifierToken(String token) {
        Algorithm algorithm = Algorithm.HMAC256(JwtTokenUtil.secret);
        Map<String, Claim> jwt;
        jwt = JWT.require(algorithm).build().verify(token).getClaims();

        return jwt;
    }



}
